<?php

namespace App\support\gateway;

use App\Console\Commands\GatewayWorker;
use GatewayWorker\Lib\Gateway;

class Events
{

    public static function onWorkerStart($businessWorker)
    {
        echo "BusinessWorker    Start\n";
    }

    public static function onConnect($client_id)
    {
        Gateway::sendToClient($client_id,  msg([
                'type' => 'init',
                'client_id' => $client_id
        ]));
        echo  "$client_id 用户已连接";
    }

    public static function onWebSocketConnect($client_id, $data)
    {
        print_r($data, $client_id);
    }

    public static function onMessage($client_id, $message)
    {
        print_r($message, $client_id);
    }

    public static function onClose($client_id)
    {
        try {
            GateWay::sendToAll(json_encode([
                'type' => 'logout',
                'client_id' => $client_id,
            ]));
        } catch (\Exception $e) {
            dd($e);
        }
    }

}

